(function(global){
    function _Size(width, height){
        this.width = width;
        this.height = height;
    }
    /**
     * 将大小控制在一定范围内，等比缩放。
     * @param {Number} maxWidth
     * @param {Number} maxHeight
     * @return {Boolean} 是否被控制过大小。
     */
    _Size.prototype.constraint = function(maxWidth, maxHeight){
        var isResized = false;
        maxWidth = maxWidth || 1000;
        maxHeight = maxHeight || 1000;
        if(this.width > maxWidth){                                
            this.height = parseInt(this.height * maxWidth / this.width);
            this.width = maxWidth; 
            isResized = true;
        }
        if(this.height > maxHeight){                                
            this.width = parseInt(this.width * maxHeight / this.height);
            this.height = maxHeight;
            isResized = true;
        }
        return isResized;
    };    
    /**
     * 限制指定对象的宽高在指定的范围内.
     * @param {Object} obj
     * @param {Number} width
     * @param {Number} height
     */
    _Size.constraint = function(obj, width, height){
        var size = new _Size(obj.width, obj.height);
        size.constraint(width, height);
        obj.width = size.width;
        obj.height = size.height;
        return obj;
    };
    
    //-------------------------------------------------------------------------
    
    //TODO 老版本兼容性代码，新项目中去掉.
    window.Size = _Size;
    
    ujs.define('Size', 'ujs', function(){
        return _Size;
    });
})(window);